const { getChainId, network } = require("hardhat")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
require("dotenv").config()

// module.exports = async function (hre) {
//  const { deployments, getNamedAccounts } = hre
// module.exports = async function ({ deployments, getNamedAccounts }) {
module.exports = async ({ deployments, getNamedAccounts }) => {
    const { deploy, log } = deployments // const deploy = deployments.deploy, log = deployments.log
    const { deployer } = await getNamedAccounts()
    const chainId = await getChainId()
    console.log("chainId: ", chainId)

    // let ethUsdPriceFeedAddress
    // if (chainId == 31337) {
    //     const ethUsdAggregator = await deployments.get("MockV3Aggregator")
    //     ethUsdPriceFeedAddress = ethUsdAggregator.address
    // } else {
    //     ethUsdPriceFeedAddress = networkConfig[chainId]["ethUsdPriceFeed"]
    // }
    const ethUsdPriceFeedAddress =
        chainId == "31337"
            ? (await deployments.get("MockV3Aggregator")).address
            : networkConfig[chainId]["ethUsdPriceFeed"]

    // if the contract doesn't exist, we deploy a minimal version of
    // for our local testing

    // well what happens when we want to change chains?
    // when going for localhost or hardhat network, we want to use a mock price feed
    log("----------------------------------------------------")
    log("Deploying FundMe and waiting for confirmations...")
    const fundMe = await deploy("FundMe", {
        from: deployer,
        // gasLimit: 4000000,
        args: [ethUsdPriceFeedAddress],
        log: true,
        waitConformations: network.config.blockConfirmations || 1,
    })
    log(`FundMe deployed at ${fundMe.address}`)

    // if we are on a live network, we want to verify the contract
    if (
        !developmentChains.includes(network.name) &&
        process.env.ETHERSCAN_API_KEY
    ) {
        await verify(fundMe.address, [ethUsdPriceFeedAddress])
    }
}
module.exports.tags = ["all", "fundme"]
